package com.huohuo.springdemo.proxy.jdk;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

public class UserServiceHandler implements InvocationHandler {
    //真实的对象
    private Object realObject;



    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("*******"+proxy.getClass().getName());//*******com.sun.proxy.$Proxy70
        User user = (User) args[0];
        if (user.getName().equals( "杨" )){
            throw new Exception( "名字不能姓杨" );
        }
        //上面是前置增强
        //method.invoke 该行调用方法本身，执行了UserServiceImpl.addUser方法,该方法返回值为空，所以这里返回值也是空
        Object object = method.invoke(realObject,args);
        //后置增强
        System.out.println("业务逻辑增强，invoke()方法执行结束");
        return object;


    }


    public UserServiceHandler(Object object){
        super();
        this.realObject = object;
    }

    public Object getObject() {
        return realObject;
    }

    public void setObject(Object object) {
        this.realObject = object;
    }
}
